<template>
  <el-button type="primary" @click="handleLoad"> 加载业务数据</el-button>
  <el-button type="primary" @click="handleAdd"> 新增业务数据</el-button>
  <el-table :data="state.list" style="margin-top: 10px">
    <el-table-column prop="orgName" label="orgName" width="180" />
    <el-table-column prop="capital" label="capital" width="180" />
    <el-table-column
      prop="legalRepresentative"
      label="legalRepresentative"
      width="180"
    />
    <el-table-column prop="introduction" label="introduction" width="180" />
    <el-table-column prop="businessScope" label="businessScope" width="180" />
    <el-table-column fixed="right" label="Operations" width="120">
      <template #default="scope">
        <el-button
          link
          type="primary"
          size="small"
          @click="handleEdit(scope.row.id)"
          >Edit</el-button
        >
        <el-button
          link
          type="primary"
          size="small"
          @click="handleDelete(scope.row.id)"
          >Delete</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <edit-form
    ref="editFormRef"
    @on-submit-success="handleSubmitSuccess"
  ></edit-form>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import apiUploadDemo from "./upload-demo";
import utils from "../../../utils/utils";
import editForm from "./edit.vue";
import { useI18n } from "vue-i18n";
import { ElMessageBox } from "element-plus";
import apiOrganization from "../../admin/api/organization";
const { t } = useI18n({ useScope: "global" });

const editFormRef = ref();
const formEdit = reactive({});

const state = reactive({
  list: Array<any>(),
});

const handleLoad = () => {
  apiUploadDemo
    .list()
    .then((response: any) => {
      if (response.data.code == 200) {
        state.list = response.data.data;
        utils.infoNotification(t("common.submitSuccess"));
      }
    })
    .catch((err: any) => {
      // 还原原文
      utils.errorNotification(t("common.submitException"), err);
    });
};

const handleSubmitSuccess = () => {
  handleLoad();
};

onMounted(() => {
  handleLoad();
});

const handleAdd = () => {
  editFormRef.value.show();
};

const handleEdit = (id: string) => {
  editFormRef.value.show(id);
};

const handleDelete = (id: any) => {
  console.log("del", id);
  ElMessageBox.confirm("确认删除吗?")
    .then(() => {
      apiUploadDemo
        .delete(id)
        .then((response: any) => {
          if (response.data.code == 200) {
            handleLoad();
          }
        })
        .catch((err: any) => {
          utils.errorNotification(t("common.submitException"), err);
        });
    })
    .catch((err: any) => {});
};
</script>
